package main

import (
    "io/ioutil"
    "log"
    "net/http"
    "sync"
    "strings"
)

var wc sync.WaitGroup

func HelloClient(client *http.Client, url string, method string){
    defer wc.Done()
    req, err := http.NewRequest(method, url, nil)
    if err != nil {
        log.Fatal(err)
    }
    rep, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    data, err := ioutil.ReadAll(rep.Body)
    rep.Body.Close()
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("%s", data)
}



func HelloClient_Post(client *http.Client, url string, method string) {
	defer wc.Done()
    if method == "POST" {
  		reqData := "name=ali&age=19"
	    req, err := http.NewRequest(method, url, strings.NewReader(reqData))
		
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	    rep, err := client.Do(req)
	    if err != nil {
	    	log.Fatal(err)
	    }
	    data, err := ioutil.ReadAll(rep.Body)
	    rep.Body.Close()
	    if err != nil {
	    	log.Fatal(err)
	    }
	    log.Printf("%s", data)
		
    } else {
  	    req, err := http.NewRequest(method, url, nil) //GET
	    if err != nil {
		    log.Fatal(err)
	    }


		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	    rep, err := client.Do(req)
	    if err != nil {
	    	log.Fatal(err)
	    }
	    data, err := ioutil.ReadAll(rep.Body)
	    rep.Body.Close()
	    if err != nil {
	    	log.Fatal(err)
	    }
	    log.Printf("%s", data)
	
	}

	
}

func main() {
    client := &http.Client{}
    rootURL := "http://127.0.0.1:9098/"
    wc.Add(1)
    go HelloClient(client, rootURL + string("login"), "GET")
    wc.Wait()

    client = &http.Client{}
    rootURL = "http://127.0.0.1:9098/post"
    wc.Add(1)
    go HelloClient_Post(client, rootURL, "POST")
    wc.Wait()
}

